package com.weasel.mybatis.property;

import com.weasel.mybatis.help.DESHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * Created by Dylan on 2015/12/18.
 */
public class DataSourcePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {


    private String[] encryptPropNames = {"jdbc.username", "jdbc.password"};

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {

        if(isEncryptProp(propertyName)){
            return DESHelper.decrypt(propertyValue);
        }
        return propertyValue;
    }

    private boolean isEncryptProp(String propertyName){

        for(String encryptPropName : encryptPropNames){
            if(StringUtils.equals(encryptPropName,propertyName)){
                return true;
            }
        }
        return false;
    }
}
